
name: iot

on:
  push:
    branches: [ "main" ]

  workflow_dispatch:

jobs:

  backbuild:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
        - name: Checkout
          uses: actions/checkout@v2     
        - name: Login to github registry
          uses: docker/login-action@v2
          with:
            registry: ghcr.io
            username: ${{ github.actor }}
            password: ${{ secrets.GITHUB_TOKEN }}
        - name: get build number
          uses: benjlevesque/short-sha@v1.2
          id: short-sha
          with:
           length: 7
        - name: Get git commit data
          uses: rlespinasse/git-commit-data-action@v1           
        - name : build docker image and push
          uses: docker/build-push-action@v3
          with:
            context: .
            file: backend/Dockerfile
            push: true
            tags: |
              ghcr.io/${{ github.repository }}/${{ github.workflow }}/iotbackend:${{ steps.short-sha.outputs.sha }}             
        # - name: Login to DockerHub
        #   uses: docker/login-action@v2
        #   with:
        #     username: ${{ secrets.DOCKERHUB_USERNAME }}
        #     password: ${{ secrets.DOCKERHUB_PASSWORD }}
        # - name: Build and push to Dockerhub
        #   uses: docker/build-push-action@v3
        #   with:
        #     context: .
        #     file: backend/Dockerfile
        #     push: true
        #     tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ github.workflow }}:${{ steps.short-sha.outputs.sha }}
        - name: Send notification
          uses: dawidd6/action-send-mail@v2
          with:
            server_address: smtp.gmail.com
            server_port: 465
            username: ${{ secrets.EMAIL_SENDER_USERNAME }}
            password: ${{ secrets.EMAIL_SENDER_PASSWORD }}
            subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
            body: ${{ github.job }} job in workflow ${{ github.workflow }} of ${{ github.repository }} of backend has ${{ job.status }}
            to: ${{ env.GIT_COMMIT_COMMITTER_EMAIL }}
            from: ${{ env.GIT_COMMIT_AUTHOR }}
  # backdeploy:
  #   runs-on: ubuntu-latest
  #   needs: [backbuild]
  #   steps:
  #   - uses: actions/checkout@master
  #   - name: deploy to cluster
  #     uses: ghostzero/kubectl@v1
  #     env:
  #       KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }} #cat $HOME/.kube/config | base64 | pbcopy
  #     with:
  #       args: |
  #           create ns ${{ github.workflow }}
  #           apply -f kubernetes-deploy/back-iot-app.yaml -n ${{ github.workflow }}

  fronttest:
    runs-on: ubuntu-latest
    steps:
        - name: Checkout
          uses: actions/checkout@v2      
        - name: Setup-node     
          uses: actions/setup-node@v2
          with:
            working-directory: frontend/angular/
            node-version: 14.x                  
        - name: Install
          working-directory: frontend/angular/
          run: npm ci
        - name: Run Test
          working-directory: frontend/angular/
          run: npm run test -- --no-watch --no-progress --browsers=ChromeHeadless  
        - name: Get git commit data
          uses: rlespinasse/git-commit-data-action@v1                    
        - name: Send notification
          uses: dawidd6/action-send-mail@v2
          if: job.status.failure
          with:
            server_address: smtp.gmail.com
            server_port: 465
            username: ${{ secrets.EMAIL_SENDER_USERNAME }}
            password: ${{ secrets.EMAIL_SENDER_PASSWORD }}
            subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
            body: ${{ github.job }} job in workflow ${{ github.workflow }} of ${{ github.repository }} of frontend has ${{ job.status }}
            to: ${{ env.GIT_COMMIT_COMMITTER_EMAIL }}"
            from: ${{ env.GIT_COMMIT_AUTHOR }}"
           
  frontbuild:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    needs: [fronttest]
    steps:
        - name: Get current time of build
          uses: gerred/actions/current-time@master
          id: current-time
        - name: Checkout
          uses: actions/checkout@v2  
        - name: Setup-node     
          uses: actions/setup-node@v2
          with:
            node-version: 14.x        
            working-directory: frontend/angular/          
        - name: Install
          working-directory: frontend/angular/
          run: npm ci
        - name: Run build
          working-directory: frontend/angular/
          run: npm run build     
        - name: Login to github registry
          uses: docker/login-action@v2
          with:
            registry: ghcr.io
            username: ${{ github.actor }}
            password: ${{ secrets.GITHUB_TOKEN }}
        - name: get build number
          uses: benjlevesque/short-sha@v1.2
          id: short-sha
          with:
           length: 7
        - name : build docker image and push
          uses: docker/build-push-action@v3
          with:
            context: .
            file: frontend/Dockerfile
            push: true
            tags: |
              ghcr.io/${{ github.repository }}/${{ github.workflow }}/iotfrontend:${{ steps.short-sha.outputs.sha }}
        - name: Get git commit data
          uses: rlespinasse/git-commit-data-action@v1               
        - name: Send notification
          uses: dawidd6/action-send-mail@v2
          with:
            server_address: smtp.gmail.com
            server_port: 465
            username: ${{ secrets.EMAIL_SENDER_USERNAME }}
            password: ${{ secrets.EMAIL_SENDER_PASSWORD }}
            subject: ${{ github.job }} job of ${{ github.repository }} has ${{ job.status }}
            body: ${{ github.job }} job in workflow ${{ github.workflow }} of ${{ github.repository }} of backend has ${{ job.status }} 
            to: ${{ env.GIT_COMMIT_COMMITTER_EMAIL }}"
            from: ${{ env.GIT_COMMIT_AUTHOR }}"  

  # frontdeploy:
  #   name: deploy to cluster
  #   runs-on: ubuntu-latest
  #   needs: [frontbuild]
  #   steps:
  #   - uses: actions/checkout@master
  #   - name: deploy to cluster
  #     uses: ghostzero/kubectl@v1
  #     env:
  #       KUBE_CONFIG_DATA: ${{ secrets.KUBE_CONFIG_DATA }} #cat $HOME/.kube/config | base64 | pbcopy
  #     with:
  #       args: |
  #           create ns ${{ github.workflow }}
  #           apply -f kubernetes-deploy/front-iot-app.yaml -n ${{ github.workflow }}
  

